/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.hiep.connect.server;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.hiep.connect.client.LoginService;
import com.hiep.connect.shared.MyUser;
import com.hiep.connect.shared.Posting;
import com.hiep.connect.shared.PostingThread;

public class LoginServiceImpl extends RemoteServiceServlet implements
		LoginService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 201204231L;

	@Override
	public MyUser login(String email, String password)
			throws IllegalArgumentException {
		MyUser currentUser = objecttifyUser(email, password);
		HttpServletRequest threadLocalRequest = this.getThreadLocalRequest();
		HttpSession session = threadLocalRequest.getSession();
		System.out.println("id = " + session.getId());
		System.out.println("\ngetMaxInactiveInterval = "
				+ session.getMaxInactiveInterval());
		while (session.getAttributeNames().hasMoreElements()) {
			System.out.println("\nAttributeNames = "
					+ session.getAttributeNames().nextElement().toString());
		}

		return currentUser;
	}

	private MyUser objecttifyUser(String email, String password) {

		MyUser currentUser = new MyUser();
		currentUser.setEmail(email);
		currentUser.setPassword(password);

		ObjectifyService.register(MyUser.class);
		Objectify ofy = ObjectifyService.begin();
		// ofy.put(currentUser);

		MyUser myUser = ofy.get(MyUser.class, email);

		return fillUser(myUser);
	}

	@Override
	public MyUser login(MyUser user) throws IllegalArgumentException {
		// TODO get user from DAO

		return login(user.getEmail(), user.getPassword());
	}

	public MyUser login(String requestUri) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		MyUser loginInfo = new MyUser();

		if (user != null) {
			loginInfo.setLoggedIn(true);
			loginInfo.setEmailAddress(user.getEmail());
			loginInfo.setNickname(user.getNickname());
			loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri));
		} else {
			loginInfo.setLoggedIn(false);
			loginInfo.setLoginUrl(userService.createLoginURL(requestUri));
		}
		return loginInfo;
	}

	private MyUser fillUser(MyUser user) {
		List<MyUser> friends = new ArrayList<MyUser>();

		List<Posting> postings = new ArrayList<Posting>();

		for (int i = 1; i <= 10; i++) {
			// Add friends.
			MyUser u = new MyUser();
			u.setEmail("friend " + i);
			friends.add(u);

			// Create a posting thread.
			Posting p = new Posting("Posting content" + i);
			postings.add(p);
		}

		PostingThread thread = new PostingThread(postings);
		List<PostingThread> postingThreads = new ArrayList<PostingThread>();
		postingThreads.add(thread);

		user.setFriends(friends);
		user.setPostingThreads(postingThreads);

		return user;
	}

}
